/**
 * @author winner
 */

var getIndexOrderAjax ="";
function getIndexOrder(){
	  	$.ajax( {
   		type : "GET",
   		contentType:"application/json",
   		dataType : 'json',
   		url: "getIndexOrder",
   		data: {},
   		async: true,
   		success : function(ret) {
			getIndexOrderAjax = ret;
			initIndexGroup();
			indexPower();
		}
	})
	
}
 

function initIndexGroup(){
//	getIndexOrder();
	var indexDay = dateindexPowerList[getTimeIndexList()];
	var selectZhibiao = "";
	for(var p in indexListOfGroup){
		var indexList = indexListOfGroup[p];
		for(var i = 0;i<indexList.length;i++){
			if(containstr(indexDay,indexList[i])){
				selectZhibiao = selectZhibiao +"<option value="+p+">"+arrayGroup[p]+"</option>";
				break;
			}
		}
	}
	$("#select_zhibiao").html(selectZhibiao);
}



function indexPower(){
	//获取后台返回的权限列表
	var option = $("#select_zhibiao").val()
	var name = indexListOfGroup[option];
	//设置权限把没有权限指标remove
	setIndex(name)
	//取页面中有权限的第一个指标
	var kk=$("#zhibiaotable>tbody>tr>td").first();
	if(kk!=null){
	var kk=(kk.attr("id").split("_"))[1];
    getinfo(parseInt(kk),"");
    }else{
    	alert("没有指标权限");
    }
}
//设置用户权限下面的指标个数
function setIndex(Str){
	var index=eval(Str);
	var indexDay = dateindexPowerList[getTimeIndexList()];
	indexDay = eval(indexDay);
	var name = new Array();
	for (var i=0; i<indexDay.length; i++) {
		if(containstr(index,indexDay[i])){
			name.push(indexDay[i])
		}
	};
	/**
	 * start如果是销售数据、会员数据修改下拉顺序
	 */
	 var newName = new Array();
	 var Select_big_type = $("#select_zhibiao").val();
	 
	 if(Select_big_type == 0){//视频数据
		 var xiaoshouIndex = getIndexOrderAjax[0];
		 for (var i = 0; i < xiaoshouIndex.length; i++) {
		 	if (containstr(name, xiaoshouIndex[i])) {
		 		newName.push(xiaoshouIndex[i]);
		 		name.remove(xiaoshouIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
	 	name = newName;
	 }
	 if(Select_big_type == 1){//销售数据
		 var xiaoshouIndex = getIndexOrderAjax[1];
		 for (var i = 0; i < xiaoshouIndex.length; i++) {
		 	if (containstr(name, xiaoshouIndex[i])) {
		 		newName.push(xiaoshouIndex[i]);
		 		name.remove(xiaoshouIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
	 	name = newName;
	 }
	 if(Select_big_type == 2){//wifi数据
		 var xiaoshouIndex = getIndexOrderAjax[2];
		 for (var i = 0; i < xiaoshouIndex.length; i++) {
		 	if (containstr(name, xiaoshouIndex[i])) {
		 		newName.push(xiaoshouIndex[i]);
		 		name.remove(xiaoshouIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
	 	name = newName;
	 }
	 if(Select_big_type == 3){//销售数据
		 var xiaoshouIndex = getIndexOrderAjax[3];
		 for (var i = 0; i < xiaoshouIndex.length; i++) {
		 	if (containstr(name, xiaoshouIndex[i])) {
		 		newName.push(xiaoshouIndex[i]);
		 		name.remove(xiaoshouIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
	 	name = newName;
	 }
	 if(Select_big_type == 4){//会员数据
		  var huiyuanIndex = getIndexOrderAjax[4];
		  for (var i = 0; i < huiyuanIndex.length; i++) {
		 	if (containstr(name, huiyuanIndex[i])) {
		 		newName.push(huiyuanIndex[i]);
		 		name.remove(huiyuanIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
		 name = newName;
	 }
	  
	 /**
	  * end如果是销售数据、会员数据修改下拉顺序
	  */
	
	var zhibiaotable = "";
	for (var i=0; i<name.length; i++) {
		var zhibiao = name[i];
		zhibiaotable = zhibiaotable +"<tr><td id='zhibiao_"+zhibiao+"' class='em35 lineh35' onclick='getinfo("+zhibiao+",this)'>"+array[zhibiao]+"</td></tr>"
	};
	$("#zhibiaotable").html(zhibiaotable);
}

//查看对象是否子array中存在字符串
function containstr(arr, obj) {
	if( arr ==null){
		 return false;  
	}
    var i = arr.length;  
    while (i--) {
        if (parseInt(arr[i]) === parseInt(obj)) {  
            return true;  
        }
    }  
    return false;  
} 

function changezhibiao(object){
	var option = $(object).val()
	var name = indexListOfGroup[option];
	setIndex(name,getTimeIndexList())
	//取页面中有权限的第一个指标
	var indexnum = $("#hdtype").val();
	$("#zhibiao_"+indexnum).addClass("lxxzselected");
	
	/**注释掉切换指标类型默认查询类型下面第一个指标
	var kk=$("#zhibiaotable>tbody>tr>td").first();
	if(kk!=null){
	var kk=(kk.attr("id").split("_"))[1];
    getinfo(parseInt(kk),"");
    }else{
    	alert("没有指标权限");
    }
	*/
}

function getTimeIndexList(){
	var timeIndex;
	var timeType =  $("#timeModel").val();
	if(timeType=="hour"){
		timeIndex ="indexHour"
	}else if(timeType=="day"){
		timeIndex ="indexDay"
	}else if(timeType=="week"){
		timeIndex ="indexWeek"
	}else if(timeType=="month"){
		timeIndex ="indexMonth"
	}else if(timeType=="year"){
		timeIndex ="indexYear"
	}
	return timeIndex;
}
//changsuo = 200 siteIndexPowerList = index200
function getSiteTypeIndexPower(changsuo){
	var changsuoType = parseInt(changsuo);
	var indexpower = ""
	switch(changsuoType) {
	case 200:
		indexpower = siteIndexPowerList["index200"]
		break;
	case 300:
		indexpower = siteIndexPowerList["index300"]
		break;
	case 400:
		indexpower = siteIndexPowerList["index400"]
		break;
	case 500:
		indexpower = siteIndexPowerList["index500"]
		break;
	case 600:
		indexpower = siteIndexPowerList["index600"]
		break;
	case 700:
		indexpower = siteIndexPowerList["index700"]
		break;
	}
    return	indexpower;
}


//排行指标权限

function setpaihangIndex(changsuo,firstType){
	var powerStr = getSiteTypeIndexPower(changsuo);//得到场所对应的指标
	var strIndex=eval(powerStr);
	initPaihangIndexGroup(strIndex,firstType)//将指标分组
	initPaihangIndex(strIndex);
	
}
function initPaihangIndexGroup(strIndex,firstType){
	
	var indexDay = dateindexPowerList[getTimeIndexList()];
	var selectZhibiao = "";
	var initGroup = "";
	for(var p in strIndex){
		
		var indexList = strIndex[p];
		for(var i = 0;i<indexList.length;i++){
			if(containstr(indexDay,indexList[i])){
				selectZhibiao = selectZhibiao +"<option value="+p+">"+arrayGroup[p]+"</option>";
				var indexList = strIndex[p]
				indexList = eval(indexList);
				if(containstr(indexList,firstType)){
					initGroup = p;
				}
				break;
			}
		}
	}
	$("#select_paihangzhibiao").html(selectZhibiao);
	$("#select_paihangzhibiao").val(initGroup);
}

function initPaihangIndex(strIndex){
	//获取后台返回的权限列表
	var option = $("#select_paihangzhibiao").val()
	var IndexPowerList =  strIndex[option];
	//添加指标
	setPaihangIndexstr(IndexPowerList)

}

function setPaihangIndexstr(IndexPowerList){
	var index=eval(IndexPowerList);
	var indexDay = dateindexPowerList[getTimeIndexList()];
	indexDay = eval(indexDay);
	
	var name = new Array();
	for (var i=0; i<indexDay.length; i++) {
		if(containstr(index,indexDay[i])){
			name.push(indexDay[i])
		}
	};
		/**
	 * start如果是销售数据、会员数据修改下拉顺序
	 */
	 var newName = new Array();
	 var Select_big_type = $("#select_paihangzhibiao").val();
 if(Select_big_type == 0){//视频数据
		 var xiaoshouIndex = getIndexOrderAjax[0];
		 for (var i = 0; i < xiaoshouIndex.length; i++) {
		 	if (containstr(name, xiaoshouIndex[i])) {
		 		newName.push(xiaoshouIndex[i]);
		 		name.remove(xiaoshouIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
	 	name = newName;
	 }
	 if(Select_big_type == 1){//销售数据
		 var xiaoshouIndex = getIndexOrderAjax[1];
		 for (var i = 0; i < xiaoshouIndex.length; i++) {
		 	if (containstr(name, xiaoshouIndex[i])) {
		 		newName.push(xiaoshouIndex[i]);
		 		name.remove(xiaoshouIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
	 	name = newName;
	 }
	 if(Select_big_type == 2){//wifi数据
		 var xiaoshouIndex = getIndexOrderAjax[2];
		 for (var i = 0; i < xiaoshouIndex.length; i++) {
		 	if (containstr(name, xiaoshouIndex[i])) {
		 		newName.push(xiaoshouIndex[i]);
		 		name.remove(xiaoshouIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
	 	name = newName;
	 }
	 if(Select_big_type == 3){//销售数据
		 var xiaoshouIndex = getIndexOrderAjax[3];
		 for (var i = 0; i < xiaoshouIndex.length; i++) {
		 	if (containstr(name, xiaoshouIndex[i])) {
		 		newName.push(xiaoshouIndex[i]);
		 		name.remove(xiaoshouIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
	 	name = newName;
	 }
	 if(Select_big_type == 4){//会员数据
		  var huiyuanIndex = getIndexOrderAjax[4];
		  for (var i = 0; i < huiyuanIndex.length; i++) {
		 	if (containstr(name, huiyuanIndex[i])) {
		 		newName.push(huiyuanIndex[i]);
		 		name.remove(huiyuanIndex[i]);
		 	}
		 }
		 for (var j = 0; j < name.length; i++) {
		 	newName.push(name[i]);
			name.remove(name[i]);
		 }
		 name = newName;
	 }
	 /**
	  * end如果是销售数据、会员数据修改下拉顺序
	  */
	var zhibiaopaihangtable = "";
	for (var i=0; i<name.length; i++) {
		var zhibiao = name[i];
		zhibiaopaihangtable = zhibiaopaihangtable + "<tr><td id='zhibiaopaihang_"+zhibiao+"' class='em35 lineh35' onclick='getRankInfo("+zhibiao+",this)'>"+array[zhibiao]+"</td></tr>"
	};
	$("#zhibiaopaihangtable").html(zhibiaopaihangtable);
}

function getSiteOfTimeIndex(changsuo){
	
	var siteIndex = getSiteTypeIndexPower(changsuo)
	var index = new Array();
	for (var p in siteIndex) {
		var pi = siteIndex[p];
		index.push(pi);
	};
	var indexDay = dateindexPowerList[getTimeIndexList()];
	indexDay = eval(indexDay);
	
	var name = new Array();
	for (var i=0; i<indexDay.length; i++) {
		if(containstr(index,indexDay[i])){
			name.push(indexDay[i])
		}
	}
	return name;
}

//排行榜指标添加底色
function setpaihangIndexcss(Str){
	$("#zhibiaopaihangtable>tbody>tr").children("td").each(function(){
			$(this).removeClass("lxxzselected");
	 });
	$("#zhibiaopaihang_"+Str).addClass("lxxzselected");
}


function  changepaihangzhibiao(Object){
	var option = $(Object).val()
	var changsuo = $("#rankingType").val();
	var siteIndex = getSiteTypeIndexPower(changsuo)
	var IndexType = siteIndex[option];
	setPaihangIndexstr(IndexType)
	
	for(var i = 0;i<arrpaihang.length;i++){
		$("#zhibiaopaihang_"+arrpaihang[i]).addClass("lxxzselected");
	}
	
	/**
	注释切换指标类型默认为第一个查询 
	//取页面中有权限的第一个指标
	var kk=$("#zhibiaopaihangtable>tbody>tr>td").first();
	if(kk!=null){
	var kk=(kk.attr("id").split("_"))[1];
    	$("#rankingIndex").val(kk);
		setpaihangIndexcss(kk);
		var data= getRankDate(changsuo);
		parseDate(changsuo,data);
    }else{
    	alert("没有指标权限");
    }
	*/	
}
